package com.chen.bq;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

// 这组API，超时等待
public class BlockingQueueTest4 {
    public static void main(String[] args) throws InterruptedException {

        // 队列大小3
        ArrayBlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        // 添加。参数依次是：要添加的元素、超时等待时间、时间单位
        System.out.println(blockingQueue.offer("a", 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("b", 2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.offer("c", 2, TimeUnit.SECONDS));
        // 等待2秒后，返回false
        // System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS));

        System.out.println("===================");

        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
        // 等待2秒后，返回null
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
    }
}
